package com.zhang.common.db;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zhang.common.config.MybatisPlusConfig;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

/**
 * 多数据源配置
 *
 * 不管管注解是sqlSessionFactoryRef还是sqlSessionTemplateRef，在使用的时候走的都是sqlSessionTemplate
 * 使用sqlSessionTemplateRef = "xxSqlSessionTemplate" 注解即可
 * @author zhangxin
 * @date 2021/5/20
 */
@Configuration
@MapperScan(basePackages = "com.zhang.common.mapper", sqlSessionTemplateRef = "defaultSqlSessionTemplate")
public class DefaultDataSourceConfig {

    @Autowired
    private MybatisPlusConfig mybatisPlusConfig;

    private DataSource defaultDataSource;

    //主数据源配置 默认数据源
    @Primary
    @Bean(name = "defaultDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSourceProperties defaultDataSourceProperties() {
        return new DataSourceProperties();
    }

    @PostConstruct
    public void init() {
        this.defaultDataSource = defaultDataSourceProperties().initializeDataSourceBuilder().build();
    }

    /**
     * 指定数据库
     *
     * @return
     * @throws Exception
     */
    @Primary
    @Bean(name = "defaultSqlSessionFactory")
    public SqlSessionFactory defaultSqlSessionFactory() throws Exception {
        // 非mybatis配置
        // final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        // ......
        // mybatis配置
        final MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(defaultDataSource);
        // mybatisPlus的分页插件
        mybatisSqlSessionFactoryBean.setPlugins(mybatisPlusConfig.mybatisPlusInterceptor());
        mybatisSqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().
                getResources("classpath:mapper/*.xml"));
        return mybatisSqlSessionFactoryBean.getObject();
    }

    /**
     * 指定数据库
     *
     * @return
     * @throws Exception
     */
    @Primary
    @Bean(name = "defaultSqlSessionTemplate")
    public SqlSessionTemplate defaultSqlSessionTemplate() throws Exception {
        return new SqlSessionTemplate(this.defaultSqlSessionFactory());
    }

    /**
     * 事务支持
     *
     * @return
     * @throws Exception
     */
    @Primary
    @Bean(name = "defaultTransactionManager")
    public DataSourceTransactionManager defaultTransactionManager() {
        return new DataSourceTransactionManager(defaultDataSource);
    }

}
